var mywork = new Vue({
    el:'#mywork',
    data:{
        /*查询中的销货清单展示*/
        dialogTableVisible: false,
        /*页面刷新时查询的表单*/
        allOrder:[],
        /*测试查询销货清单弹框*/
        gridData: [{
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }, {
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }],
        /*查询第一部分的定义*/
        ruleForm:{
            /*客户名称*/
            ruleName:'',
            /*车牌汉字*/
            ruleCarWord:'',
            /*车牌英文*/
            ruleCarEng:'',
            /*车牌号*/
            ruleCarCode:'',
            /*车辆品牌*/
            ruleType:'',
            /*配件名称*/
            rulePartName:'',
            /*维修类型*/
            ruleType:'',
            /*经办人*/
            ruleHandle:'',
            /*维修人*/
            rulePart:'',
            /*维修时间*/
            ruleTime:'',
        },
        /*新增的字段*/
        addForm:{
            /*客户名称*/
            clientName:'',
            /*客户电话*/
            clientPhone:'',
            /*客户车牌号*/
            clientCarCode:'',
            /*车辆品牌*/
            clientCarBrand:'',
            /*车架号*/
            clientShelfCode:'',
            /*车辆年限*/
            clientCarYear:'',
            /*车辆排量*/
            clientCarPlace:'',
            /*销货清单文本框*/
            clientOrderList:'',
            /*维修类型*/
            clientCarType:'',
            /*公里数*/
            clientCarKilo:'',
            /*经办人*/
            clientCarAgent:'',
            /*维修人*/
            clientCarMain:'',
            /*付款方式*/
            clientCarPay:'',
            /*维修时间*/
            clientCarTime:'',
            /*图片上传*/
            clientOrderPicture:'',
            /*备注*/
            clientCarRemark:'',
        },
        addFormRef:{
            clientName:[{required: true, message: '请输入客户名称', trigger: 'blur' }],
            clientPhone:[{required: true, message: '请输入联系电话', trigger: 'blur' },{min: 11, max: 11, message: '联系电话最少11位'}],
            clientCarCode:[{required: true, message: '请输入车牌号码', trigger: 'blur' },{min: 7, max: 7, message: '车牌号码为7位'}],
            clientCarBrand:[{required: true, message: '请选择车辆品牌', trigger: 'change' }],
            clientShelfCode:[{required: true, message: '请输入车架号', trigger: 'blur' }],
            clientCarYear:[{required: true, message: '请输入车辆年限', trigger: 'blur' },{type: 'number',message: '车辆年限只能为数字'}],
            clientCarPlace:[{required: true, message: '请输入车辆排量', trigger: 'blur' }],
            clientCarType:[{required: true, message: '请输入维修类型', trigger: 'blur' }],
            clientCarKilo:[{required: true, message: '请输入公里数字', trigger: 'blur' },{type: 'number',message: '公里数只能为数字'}],
            clientCarAgent:[{required: true, message: '请输入经办人姓名', trigger: 'blur' }],
            clientCarMain:[{required: true, message: '请输入维修人姓名', trigger: 'blur' }],
            clientCarPay:[{required: true, message: '请选择付款方式', trigger: 'change' }],
            clientCarTime:[{required: true,type: 'date',  message: '请选择维修时间', trigger: 'change' }],
            clientOrderList:[{required: true, message: '请填写销货清单', trigger: 'change' }],
        },
        saleForm:[{partsname:'',
            partsnum:'0',
            partsunit:'',
            partsprice:'0',
            partstotal:'0',
        }],
        activeName:'0',
        count:0,
        number:1,
        disTFlse:false,
        dialogFormVisible:false,
        dialogFormVise:false,
        dialogFormPicture:false,
        map:{},
        pictImageUrl:'/repair_order/image_upload',
        fileList:[{name:'feign.jpg',url:'/master/images/projeIges/feign.jpg'}],
        restaurants: [{ "value": "三全鲜食（北新泾店）", "address": "长宁区新渔路144号" },
{ "value": "Hot honey 首尔炸鸡（仙霞路）", "address": "上海市长宁区淞虹路661号" },
{ "value": "新旺角茶餐厅", "address": "上海市普陀区真北路988号创邑金沙谷6号楼113" },
{ "value": "泷千家(天山西路店)", "address": "天山西路438号" },
{ "value": "胖仙女纸杯蛋糕（上海凌空店）", "address": "上海市长宁区金钟路968号1幢18号楼一层商铺18-101" },
{ "value": "贡茶", "address": "上海市长宁区金钟路633号" },
{ "value": "豪大大香鸡排超级奶爸", "address": "上海市嘉定区曹安公路曹安路1685号" },
{ "value": "茶芝兰（奶茶，手抓饼）", "address": "上海市普陀区同普路1435号" },
{ "value": "十二泷町", "address": "上海市北翟路1444弄81号B幢-107" },
{ "value": "星移浓缩咖啡", "address": "上海市嘉定区新郁路817号" },
{ "value": "阿姨奶茶/豪大大", "address": "嘉定区曹安路1611号" },
{ "value": "新麦甜四季甜品炸鸡", "address": "嘉定区曹安公路2383弄55号" },
{ "value": "Monica摩托主题咖啡店", "address": "嘉定区江桥镇曹安公路2409号1F，2383弄62号1F" },
{ "value": "浮生若茶（凌空soho店）", "address": "上海长宁区金钟路968号9号楼地下一层" },
{ "value": "NONO JUICE  鲜榨果汁", "address": "上海市长宁区天山西路119号" },],


    },

    methods: {
        /*更多按钮的单击方法*/
        activeClick: function () {
            var _this = this;
            debugger
            if (_this.activeName == '0' || _this.activeName == '2') {
                _this.activeName = '1';
            } else {
                _this.activeName = '2';
            }

        },
        /*查询方法*/
        findHandleOrder:function(){
            var _this = this;
            debugger
            $.post("/repair_order/repair_find",{"result":JSON.stringify(_this.ruleForm)},function(data){

            });
        },
        /*打开模态窗口*/
        dialogForm:function(type){
            var _this = this;
            debugger
            if(type==1){
             _this.dialogFormVisible=true;
             _this.addForm = {};
            }else{
                _this.dialogFormVise = true;
                if( _this.count!=0){
                    _this.saleForm = [];
                    _this.saleForm.push({partName:'',partNum:'0', partUnit:'',partPrice:'0',totlePrice:'',});
                }


            }
        },
        /*图片伪装上传判断按钮*/
        submitPicture:function(){
            var _this = this;
            debugger
            if(_this.fileList.length==1&&_this.fileList[0].name=="feign.jpg"){
                _this.$message({
                    /*showClose: true,*/
                    message: '警告哦，请先选取图片文件！',
                    type: 'warning'
                });
            }else{
                _this.dialogFormPicture = false;
                _this.$message({
                    /*showClose: true,*/
                    message: '恭喜，图片已提交！',
                    type: 'success'
                });
                _this.$refs.upload.submit();
            }

        },

        handleRemove:function(file, fileList){
            var _this = this;
            debugger
            for(var i=0;i<_this.fileList.length;i++){
                if(file.name==_this.fileList[i].name||file.url==_this.fileList[i].url){
                    _this.fileList.splice(i,1);
                }
            }

        },
        handleChange:function(file, fileList){
            var _this = this;
            debugger
            var  cout=0;
            if(fileList[0].name=="feign.jpg"){
                fileList.splice(0,1);
                _this.fileList.splice(0,1);
            }
            var num = fileList.length;
            for(var i=0;i<fileList.length;i++){
               if(fileList[num-1].name==fileList[i].name||fileList[num-1].url==fileList[i].url){
                   cout+=1;
                   if(cout==2){
                       fileList.splice(num-1,1);
                       _this.fileList.splice(num-1,1);
                       _this.$message({
                           message: '警告哦，该图片已经添加！',
                           type: 'warning'
                       });
                   }else{
                       _this.fileList.push(file);
                   }

               }
            }
            const isJPG = file.raw.type === 'image/jpeg'||file.raw.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                fileList.splice(num-1,1);
                _this.fileList.splice(num-1,1);
                this.$message.error('上传图片只能是JPG或者PNG格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        /*新增/删除一行销货清单数据 type=1:新增行，type=2：删除行*/
        AddDeleteRow:function(index,type){
            var _this = this;
            debugger
            switch (type){
                case 1:
                    if(_this.saleForm[index].partName==''){
                        _this.$message({
                            /*showClose: true,*/
                            message: '警告哦，请先填写配件名称！',
                            type: 'warning'
                        });
                    }else if(_this.saleForm.length!=index+1){
                        _this.$message({
                            /*showClose: true,*/
                            message: '警告哦，请在第'+_this.saleForm.length+'添加新行！',
                            type: 'warning'
                        });
                    }else if(_this.saleForm[index].partNum==0||_this.saleForm[index].partPrice==0){
                        _this.$message({
                            /*showClose: true,*/
                            message: '警告哦，数量或单价未输入！',
                            type: 'warning'
                        });
                    }else{
                        _this.saleForm.push({partName:'',partNum:'0', partUnit:'',partPrice:'0',totlePrice:'',});
                    }
                    break;
                default:
                    if(index==0){
                        _this.$message({
                            /*showClose: true,*/
                            message: '警告哦，最后一条记录不可以删除！',
                            type: 'warning'
                        });
                    }else{
                        _this.saleForm.splice(index,1);
                    }
                    break;
            }
        },
        /*模糊搜索*/
        querySearch:function(queryString, query) {
            var _this = this;
            var restaurants = _this.restaurants;
            var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
            // 调用 callback 返回建议列表的数据
            query(results);
        },
        /*模糊搜索*/
        createFilter:function(queryString) {
            return function(restaurant){
                return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
            };

        },
        /*图片上传方法 0:车牌号，1:图片上传*/
        inputUpEnab:function(type){
            var _this = this;
            debugger
            switch (type){
                case 0:
                    if(_this.disTFlse){
                        _this.$message({
                            message: '警告哦，图片已上传，禁止修改车牌号！',
                            type: 'warning'
                        });
                    }
                    break;
                case 1:
                    if(_this.addForm.clientCarCode==""||_this.addForm.clientCarCode==null){
                        _this.$message({
                            message: '警告哦，请先输入车牌号',
                            type: 'warning'
                        });
                    }else{
                        _this.dialogFormPicture = true;
                    }
                    break;
                default:
                    break;
            }

        },
        /*提交销货清单数据*/
        submitOrder:function(){
            var _this = this;
            debugger
            _this.addForm.clientOrderList = "已添加"+_this.saleForm.length+"条销货清单数据";
            _this.dialogFormVise = false;
        },
        /*提交维修清单*/
        submitForm:function(addForms){
            var _this = this;
            _this.count+=1;
            _this.$refs.upload.clearFiles();
            var fileNamed = "";
            for(var i=0;i<_this.fileList.length;i++){

                if(i==_this.fileList.length-1){
                    fileNamed = fileNamed+_this.fileList[i].name;

                }else{

                    fileNamed = fileNamed+_this.fileList[i].name+",";
                }
            }
            debugger
            _this.$refs[addForms].validate(function(valid){
                if (valid) {
                    _this.addForm.clientCarTime = Date.parse(_this.addForm.clientCarTime);
                    $.post("/repair_order/repair_insert",{"addForm":JSON.stringify(_this.addForm),"saleForm":JSON.stringify(_this.saleForm),"fileNames":JSON.stringify(_this.fileNamed)},function(){

                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

    },
    created:function(){
        var _this = this;
        $.post("/repair_order/select_allOrder","",function (allOrders) {
            debugger;
            _this.allOrder = allOrders.data;
        })
    }
});